package com.music.controller;

import com.github.pagehelper.PageInfo;
import com.music.dto.Result;
import com.music.entity.Comment;
import com.music.entity.CommentLike;
import com.music.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.UUID;

@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {
    @Autowired
    private CommentService commentService;
    @PostMapping("/queryConditions")
    public @ResponseBody Result getComment(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam boolean time,String songId,String userId,String commentId){
        Result result=new Result();
        Comment comment=new Comment();
        if (songId!=null&&!"".equals(songId)){
            comment.setSongId(songId);
        }
        if (userId!=null&&!"".equals(userId)){
            comment.setUserId(userId);
        }
        if (commentId!=null&&!"".equals(commentId)){
            comment.setCommentId(commentId);
        }
        PageInfo<Comment> pageInfo=commentService.getCommentByPage(pageNum,pageSize,comment,time);
        if (!pageInfo.getList().isEmpty()){
            result.ok(pageInfo);
        }else{
            result.fail(0,"没有相关数据");
        }
        return result;
    }

    @PostMapping("/addComment")
    public @ResponseBody Result addComment(@RequestParam String songId, @RequestParam String userId,@RequestParam String commentText,String commentId){
        Result result=new Result();
        Comment comment=new Comment();
        if (commentId!=null&&!"".equals(commentId)){
            comment.setReplyId(commentId);
        }
        comment.setCommentId(UUID.randomUUID().toString().replace("-",""));
        comment.setCommentText(commentText);
        comment.setCommentStatus(1);
        comment.setSongId(songId);
        comment.setUserId(userId);
        LocalDateTime now=LocalDateTime.now();
        comment.setCommentTime(now.toString());
        boolean flag=commentService.addComment(comment);
        if(flag){
            result.ok(true);
        }else{
            result.fail(0,"插入失败");
        }
        return result;
    }

    @PostMapping("/doCommentLike")
    public @ResponseBody Result addCommentLike(@RequestParam String commentId, @RequestParam String userId) {
        Result result = new Result();
        CommentLike commentLike=new CommentLike();
        commentLike.setCommentId(commentId);
        commentLike.setUserId(userId);
        commentLike.setCommentLikeId(UUID.randomUUID().toString().replace("-",""));
        int n=commentService.doCommentLike(commentLike);
        if (n!=2){
            result.ok(n);
        }else{
            result.fail(0,"操作失败");
        }
        return result;
    }
}
